
clear all
************************************
*Numeric Export disabled
local TEX ""
local TEXON 0 //Change "0" to "1" to enable exporting numeric results to .txt files
if !`TEXON' local TEX "*"
*Also need to install texresults2
*ssc install texresults2
************************************
************************************
*Figure Export disabled
local FIG ""
local FIGON 0 //Change "0" to "1" to enable exporting figures (RELEVANT FOLDERS MUST FIRST BE CREATED)
if !`FIGON' local FIG "*"
************************************

******************************************
*Set working directory
******************************************
 
*Load Gilens' data, recode variables
do "Enns_SociologicalScience_Reproduction\LoadGilensDataRecodeConfirmAccuracy.do"

******************************************************************************************
***TAKE INTO ACCOUNT WHETHER ORIGINAL VARIABLE WAS 3 CATEGORY, 5 CATEGORY, OR 7 CATEGORY
**Calculate Percent Favor Among Dems, Reps, & Ind
gen pid_cat = 3
recode pid_cat 3=4 if pid4_fav!=0 & pid5_fav==0 & pid6_fav==0 & pid7_fav==0
recode pid_cat 3=4 if pid4_fav!=. & pid4_fav!=0 & pid5_fav==. & pid6_fav==. & pid7_fav==. 
recode pid_cat 3=5 if pid4_fav!=0 & pid5_fav!=0 & pid6_fav==0 & pid7_fav==0
recode pid_cat 3=5 if pid4_fav!=. & pid5_fav!=. & pid4_fav!=0 & pid5_fav!=0 & pid6_fav==. & pid7_fav==.
recode pid_cat 3=6 if pid4_fav!=0 & pid5_fav!=0 & pid6_fav!=0 & pid7_fav==0
recode pid_cat 3=6 if pid4_fav!=. & pid5_fav!=. & pid6_fav!=. & pid4_fav!=0 & pid5_fav!=0 & pid6_fav!=0 & pid7_fav==.
recode pid_cat 3=7 if pid7_fav!=. & pid7_fav!=0

gen perfav_rep = .
replace perfav_rep = pid1_fav/(pid1_fav+pid1_opp+pid1_dk) if pid_cat==3
*recode so all responses in direction of policy change (following Gilens)
replace perfav_rep = pid1_opp/(pid1_fav+pid1_opp+pid1_dk) if pid_cat==3 & switcher==1

replace perfav_rep = (pid1_fav+pid2_fav)/(pid1_fav+pid1_opp+pid1_dk+pid2_fav+pid2_opp+pid2_dk) if pid_cat==5
*recode so all responses in direction of policy change (following Gilens)
replace perfav_rep = (pid1_opp+pid2_opp)/(pid1_fav+pid1_opp+pid1_dk+pid2_fav+pid2_opp+pid2_dk) if pid_cat==5 & switcher==1

replace perfav_rep = (pid1_fav+pid2_fav+pid3_fav)/(pid1_fav+pid1_opp+pid1_dk+pid2_fav+pid2_opp+pid2_dk+pid3_fav+pid3_opp+pid3_dk) if pid_cat==7
*recode so all responses in direction of policy change (following Gilens)
replace perfav_rep = (pid1_opp+pid2_opp+pid3_opp)/(pid1_fav+pid1_opp+pid1_dk+pid2_fav+pid2_opp+pid2_dk+pid3_fav+pid3_opp+pid3_dk) if pid_cat==7 & switcher==1

gen perfav_rep_lor = log(perfav_rep/(1- perfav_rep))


gen perfav_dem = .
replace perfav_dem = pid3_fav/(pid3_fav+pid3_opp+pid3_dk) if pid_cat==3
*recode so all responses in direction of policy change (following Gilens)
replace perfav_dem = pid3_opp/(pid3_fav+pid3_opp+pid3_dk) if pid_cat==3 & switcher==1


replace perfav_dem = (pid5_fav+pid4_fav)/(pid5_fav+pid5_opp+pid5_dk+pid4_fav+pid4_opp+pid4_dk) if pid_cat==5
*recode so all responses in direction of policy change (following Gilens)
replace perfav_dem = (pid5_opp+pid4_opp)/(pid5_fav+pid5_opp+pid5_dk+pid4_fav+pid4_opp+pid4_dk) if pid_cat==5 & switcher==1


replace perfav_dem = (pid7_fav+pid6_fav+pid5_fav)/(pid7_fav+pid7_opp+pid7_dk+pid6_fav+pid6_opp+pid6_dk+pid5_fav+pid5_opp+pid5_dk) if pid_cat==7
*recode so all responses in direction of policy change (following Gilens)
replace perfav_dem = (pid7_opp+pid6_opp+pid5_opp)/(pid7_fav+pid7_opp+pid7_dk+pid6_fav+pid6_opp+pid6_dk+pid5_fav+pid5_opp+pid5_dk) if pid_cat==7 & switcher==1

gen perfav_dem_lor = log(perfav_dem/(1- perfav_dem))


gen perfav_ind = .
replace perfav_ind = pid2_fav/(pid2_fav+pid2_opp+pid2_dk) if pid_cat==3
*recode so all responses in direction of policy change (following Gilens)
replace perfav_ind = pid2_opp/(pid2_fav+pid2_opp+pid2_dk) if pid_cat==3 & switcher==1


replace perfav_ind = (pid3_fav)/(pid3_fav+pid3_opp+pid3_dk) if pid_cat==5
*recode so all responses in direction of policy change (following Gilens)
replace perfav_ind = (pid3_opp)/(pid3_fav+pid3_opp+pid3_dk) if pid_cat==5 & switcher==1


replace perfav_ind = (pid4_fav)/(pid4_fav+pid4_opp+pid4_dk) if pid_cat==7
*recode so all responses in direction of policy change (following Gilens)
replace perfav_ind = (pid4_opp)/(pid4_fav+pid4_opp+pid4_dk) if pid_cat==7 & switcher==1

gen perfav_ind_lor = log(perfav_ind/(1- perfav_ind))

********************************************

*generate dichotomous variables to capture group intercent
gen rep_ind = .
recode rep_ind .=1 if (perfav_rep - perfav_ind)>.1 & perfav_rep!=. & perfav_ind!=.
recode rep_ind .=0 if (perfav_ind-perfav_rep)>.1 & perfav_rep!=. & perfav_ind!=.

gen rep_dem = .
recode rep_dem .=1 if (perfav_rep - perfav_dem)>.1 & perfav_rep!=. & perfav_dem!=.
recode rep_dem .=0 if (perfav_dem - perfav_rep)>.1 & perfav_rep!=. & perfav_dem!=.
****************************************************

*********************************************
*Table A-4
*********************************************

****************************************************
*Table A-4, Column 1
*Gilens approach: Pref gap b/t Reps and and Inds
****************************************************
*Reps
logit policyadopted perfav_rep_lor  if  abs(perfav_rep - perfav_ind)>.10
estimates store modelrep
mat def results = r(table)
local coefrep = results[1,1]
local serep = results[2,1]
local consrepind = results[1,2]
local consrepindse = results[2,2]
local nrepind = e(N)

*Inds  
logit policyadopted perfav_ind_lor if  abs(perfav_ind - perfav_rep)>.1 
estimates store modelind
mat def results = r(table)
local coefind = results[1,1]
local seind = results[2,1]
local consindrep = results[1,2]
local consindrepse = results[2,2]
local nindrep = e(N)
****************************************************
*Test that coef for Rep dif than Independentsuest model1 model2, cluster(area_title)
suest modelrep modelind
test [modelrep_policyadopted]perfav_rep_lor = [modelind_policyadopted]perfav_ind_lor
local pval = r(p)
***

*Dems (Dems vs. Reps)
logit policyadopted perfav_dem_lor  if  abs(perfav_dem - perfav_rep)>.1
mat def results = r(table)
local coefdem = results[1,1]
local sedem = results[2,1]
local consdemrep = results[1,2]
local consdemrepse = results[2,2]
local ndemrep = e(N)
****************************************************



****************************************************
*Table A-4, Column 2
*Correcting for group intercept: Pref gap b/t Reps and Dems and b/t Reps and Inds
****************************************************
*Reps
logit policyadopted perfav_rep_lor rep_ind if  abs(perfav_ind - perfav_rep)>.1
estimates store repg
mat def results = r(table)
local coefrepb = results[1,1]
local serepb = results[2,1]
local coefrepdem = results[1,2]
local serepdem = results[2,2]
local consrepindb = results[1,3]
local consrepindseb = results[2,3]
local nrepindb = e(N)

*Inds
logit policyadopted perfav_ind_lor rep_ind if  abs(perfav_ind - perfav_rep)>.1 
mat def results = r(table)
local coefindb = results[1,1]
local seindb = results[2,1]
local coefrepind = results[1,2]
local serepind = results[2,2]
local consindrepb = results[1,3]
local consindrepseb = results[2,3]
local nindrepb = e(N)

*Dems
logit policyadopted perfav_dem_lor rep_dem if  abs(perfav_dem - perfav_rep)>.1
estimates store demg
mat def results = r(table)
local coefdemb = results[1,1]
local sedemb = results[2,1]
local coefrepdemb = results[1,2]
local serepdemb = results[2,2]
local consdemrepb = results[1,3]
local consdemrepseb = results[2,3]
local ndemrepb = e(N)
****************************************************
*Test that coef for Rep dif than Independentsuest model1 model2, cluster(area_title)
suest repg demg
test [repg_policyadopted]perfav_rep_lor = [demg_policyadopted]perfav_dem_lor
local pvaldemrep = r(p)
***

*************************************************************
**FIGURE A-5: Predicted Responsiveness to Partisan Groups 
**When analyzing preference gaps
*************************************************************
gen rep_perc = 100*perfav_rep
gen ind_perc = 100*perfav_ind
gen dem_perc = 100*perfav_dem

*************************************************************
**Republicans
*************************************************************
logit policyadopted perfav_rep_lor  if  abs(perfav_rep - perfav_ind)>.10
mat define b = get(_b)
gen yhatrep=exp(b[1,2]+(b[1,1]*perfav_rep_lor))/(1+exp(b[1,2]+(b[1,1]*perfav_rep_lor))) if e(sample)

logit policyadopted perfav_rep_lor  if (perfav_rep - perfav_ind)>.10
mat define b = get(_b)
gen yhatrep_greater=exp(b[1,2]+(b[1,1]*perfav_rep_lor))/(1+exp(b[1,2]+(b[1,1]*perfav_rep_lor))) if e(sample)
mat drop b

logit policyadopted perfav_rep_lor  if (perfav_ind - perfav_rep)>.10
mat define b = get(_b)
gen yhatrep_lesser=exp(b[1,2]+(b[1,1]*perfav_rep_lor))/(1+exp(b[1,2]+(b[1,1]*perfav_rep_lor))) if e(sample)

twoway scatter yhatrep rep_perc, msize(small) mcolor(black) ///
|| scatter yhatrep_greater rep_perc, mcolor(orange%80) ///
|| scatter yhatrep_lesser rep_perc, mcolor(navy%90) ///
lcolor(black) xtitle("Percent Favoring Change (Republicans)", size(vlarge)) ///
ytitle("Predicted Probability of Policy Change", size(vlarge)) graphregion(fcolor(white) lcolor(white)) ///
plotregion(lcolor(black) lwidth(medium)) xlabel(10[10]90, nogrid notick labsize(large)) xscale(r(5 95) lwidth(none)) yscale(r(0 1) lwidth(none)) ylabel(0(.2)1, nogrid notick labsize(large)) ///
legend(off) ///
ysize(3) xsize(4) ///
text(.17 65 "Independents more" "supportive than Republicans" "(Independent - Republican)>10%", size(vlarge)) ///
text(.6 38 "Republicans more" "supportive than Independents" "(Republican - Independent)>10%", size(vlarge)) ///
text(.5 88.5 "{it:Combined}", size(vlarge))

`FIG'graph export AppendixFigures/predicted_rep.eps, replace
`FIG'graph export AppendixFigures/predicted_rep.pdf, as(pdf) replace
*************************************************************
*************************************************************


*************************************************************
*Indpendents
*************************************************************
logit policyadopted perfav_ind_lor  if  abs(perfav_rep - perfav_ind)>.10
mat define b = get(_b)
gen yhatind=exp(b[1,2]+(b[1,1]*perfav_ind_lor))/(1+exp(b[1,2]+(b[1,1]*perfav_ind_lor))) if e(sample)

logit policyadopted perfav_ind_lor  if (perfav_rep - perfav_ind)>.10
mat define b = get(_b)
gen yhatind_lesser=exp(b[1,2]+(b[1,1]*perfav_ind_lor))/(1+exp(b[1,2]+(b[1,1]*perfav_ind_lor))) if e(sample)
mat drop b

logit policyadopted perfav_ind_lor  if (perfav_ind - perfav_rep)>.10
mat define b = get(_b)
gen yhatind_greater=exp(b[1,2]+(b[1,1]*perfav_ind_lor))/(1+exp(b[1,2]+(b[1,1]*perfav_ind_lor))) if e(sample)

twoway scatter yhatind ind_perc, msize(small) mcolor(black) ///
|| scatter yhatind_greater ind_perc, mcolor(navy%90) ///
|| scatter yhatind_lesser ind_perc, mcolor(orange%80) ///
lcolor(black) xtitle("Percent Favoring Change (Independents)", size(vlarge)) ///
ytitle("") graphregion(fcolor(white) lcolor(white)) ///
plotregion(lcolor(black) lwidth(medium)) xlabel(10[10]90, nogrid notick labsize(large)) xscale(r(5 95) lwidth(none)) yscale(r(0 1) lwidth(none)) ylabel(0(.2)1, nogrid notick labsize(large)) ///
legend(off) ///
ysize(3) xsize(4) ///
text(.13 68 "Independents more" "supportive than Republicans" "(Independent - Republican)>10%", size(vlarge)) ///
text(.64 36 "Republicans more" "supportive than Independents" "(Republican - Independent)>10%", size(vlarge)) ///
text(.44 64 "{it:Combined}", size(vlarge)) 
`FIG'graph export AppendixFigures/predicted_ind.eps, replace
`FIG'graph export AppendixFigures/predicted_ind.pdf, as(pdf) replace

*************************************************************
*************************************************************


*************************************************************
*Democrats
*************************************************************
logit policyadopted perfav_dem_lor  if  abs(perfav_dem - perfav_rep)>.1
mat define b = get(_b)
gen yhatdem=exp(b[1,2]+(b[1,1]*perfav_dem_lor))/(1+exp(b[1,2]+(b[1,1]*perfav_dem_lor))) if e(sample)

logit policyadopted perfav_dem_lor  if (perfav_rep - perfav_dem)>.10
mat define b = get(_b)
gen yhatdem_lesser=exp(b[1,2]+(b[1,1]*perfav_dem_lor))/(1+exp(b[1,2]+(b[1,1]*perfav_dem_lor))) if e(sample)
mat drop b

logit policyadopted perfav_dem_lor  if (perfav_dem - perfav_rep)>.10
mat define b = get(_b)
gen yhatdem_greater=exp(b[1,2]+(b[1,1]*perfav_dem_lor))/(1+exp(b[1,2]+(b[1,1]*perfav_dem_lor))) if e(sample)

twoway scatter yhatdem dem_perc, msize(small) mcolor(black) ///
|| scatter yhatdem_greater dem_perc, mcolor(navy%90) ///
|| scatter yhatdem_lesser dem_perc, mcolor(orange%80) ///
lcolor(black) xtitle("Percent Favoring Change (Democrats)", size(vlarge)) ///
ytitle("") graphregion(fcolor(white) lcolor(white)) ///
plotregion(lcolor(black) lwidth(medium)) xlabel(10[10]90, notick nogrid labsize(large)) xscale(r(5 95) lwidth(none)) yscale(r(0 1) lwidth(none)) ylabel(0(.2)1, nogrid notick labsize(large)) ///
legend(off) ///
ysize(3) xsize(4) ///
text(.18 68 "Democrats more" "supportive than Republicans" "(Democrat - Republican)>10%", size(vlarge)) ///
text(.6 37 "Republicans more" "supportive than Democrats" "(Republican - Democrat)>10%", size(vlarge)) ///
text(.41 63 "{it:Combined}", size(vlarge))
`FIG'graph export AppendixFigures/predicted_dem.eps, replace
`FIG'graph export AppendixFigures/predicted_dem.pdf, as(pdf) replace


*Export results for Reps
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(coefrep) result(`coefrep') replace //coef
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(serep) result(`serep') append //se
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(consrepind) result(`consrepind') append //cons
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(consrepindse) result(`consrepindse') append // cons se 
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(nrepind) result(`nrepind') round(0) append
*Export results for Inds
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(consindrep) result(`consindrep') append //cons
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(consindrepse) result(`consindrepse') append // cons se 
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(nindrep) result(`nindrep') round(0) append
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(coefind) result(`coefind') append //coef
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(seind) result(`seind') append //se
*Export *Test that coef for Rep dif than Independentsuest model1 model2, cluster(area_title)
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(pval) result(`pval') round(3) append //p-value
*Export Dems
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(coefdem) result(`coefdem') append //coef
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(sedem) result(`sedem') append //se
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(consdemrep) result(`consdemrep') append //cons
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(consdemrepse) result(`consdemrepse') append // cons se 
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(ndemrep) result(`ndemrep') round(0) append
*Group Intercept: Reps
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(coefrepb) result(`coefrepb') append //coef
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(serepb) result(`serepb') append //se
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(coefrepdem) result(`coefrepdem') append //coef
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(serepdem) result(`serepdem') append //se
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(consrepindb) result(`consrepindb') append //cons
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(consrepindseb) result(`consrepindseb') append // cons se 
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(nrepindb) result(`nrepindb') round(0) append
*Group Intercept: Inds
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(coefindb) result(`coefindb') append //coef
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(seindb) result(`seindb') append //se
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(coefrepind) result(`coefrepind') append //coef
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(serepind) result(`serepind') append //se
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(consindrepb) result(`consindrepb') append //cons
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(consindrepseb) result(`consindrepseb') append // cons se 
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(nindrepb) result(`nindrepb') round(0) append
*Group Inercept: Dems
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(coefdemb) result(`coefdemb') append //coef
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(sedemb) result(`sedemb') append //se
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(coefrepdemb) result(`coefrepdemb') append //coef
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(serepdemb) result(`serepdemb') append //se
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(consdemrepb) result(`consdemrepb') append //cons
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(consdemrepseb) result(`consdemrepseb') append // cons se 
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(ndemrepb) result(`ndemrepb') round(0) append
*Export *Test that coef for Rep dif than Independentsuest model1 model2, cluster(area_title)
`TEX'texresults2 using TxtFiles_NumericalResults\pid.txt, texmacro(pvaldemrep) result(`pvaldemrep') round(3) append //p-value



